package com.lb.shopguide.adapter;

import android.graphics.Paint;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.lb.shopguide.R;
import com.lb.shopguide.constants.AppConstant;
import com.lb.shopguide.entity.order.GoodsInOrderBean;
import com.lb.shopguide.entity.order.OrderListBean;
import com.lb.shopguide.util.pic.ImageLoaderUtils;
import com.yqritc.recyclerviewflexibledivider.FlexibleDividerDecoration;

import java.util.List;

/**
 * Created by kang on 2017/6/3.
 */

public class AdapterOrderList extends BaseQuickAdapter<OrderListBean, BaseViewHolder> implements FlexibleDividerDecoration.PaintProvider {
    public AdapterOrderList(int layoutResId, List<OrderListBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, final OrderListBean item) {
        View view = helper.itemView;
        TextView tvStatus = view.findViewById(R.id.tv_status);
        TextView tvCloseOrder = view.findViewById(R.id.tv_close_order);
        TextView tvCheckDelivery = view.findViewById(R.id.tv_check_delivery);
        TextView tvDeliveryGoods = view.findViewById(R.id.tv_deliver_goods);
        TextView tvChangePrice = view.findViewById(R.id.tv_change_price);
        TextView tvOrderName = view.findViewById(R.id.tv_order_name);
        TextView tvTime = view.findViewById(R.id.tv_time);
        ImageView ivAvatar = view.findViewById(R.id.iv_avatar);
//        RelativeLayout layoutBottom = (RelativeLayout) view.findViewById(R.id.layout_order_bottom);
        RecyclerView recyclerView = view.findViewById(R.id.recyclerView);

//        layoutBottom.setVisibility(View.GONE);
        tvCloseOrder.setVisibility(View.GONE);
        tvCheckDelivery.setVisibility(View.GONE);
        tvDeliveryGoods.setVisibility(View.GONE);

        ImageLoaderUtils.displayCircle(mContext, ivAvatar, item.getHeadPicUrl());
        if (!item.isTourist()) {
            tvOrderName.setText(item.getNickName() + "(" + item.getTrueName() + ")");
        } else {
            tvOrderName.setText("游客");
        }
        tvTime.setText(item.getCreateTime());
        tvStatus.setTextColor(mContext.getResources().getColor(R.color.sg_black));


        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        AdapterGoodsInOrderCommon mAdapterGoodsInOrderCommon = new AdapterGoodsInOrderCommon(R.layout.layout_goods_base_order, item.getOrderDetailList());
        recyclerView.setAdapter(mAdapterGoodsInOrderCommon);
        switch (item.getOrderType()) {
            case AppConstant.ORDER_TYPE_SS_EXPRESS://自商城快递
                switch (item.getOrderStatus()) {
                    case AppConstant.ORDER_STATUS_NO_PAY:
                        tvStatus.setText("待支付");
                        tvCloseOrder.setVisibility(View.VISIBLE);
//                        tvChangePrice.setVisibility(View.VISIBLE);
                        break;
                    case AppConstant.ORDER_STATUS_NO_DELIVER:
                        tvStatus.setText("待发货");
                        tvDeliveryGoods.setVisibility(View.VISIBLE);
                        break;
                    case AppConstant.ORDER_STATUS_NO_RECEIVE:
                        tvStatus.setText("已发货");
                        tvCheckDelivery.setVisibility(View.VISIBLE);
                        break;
                    case AppConstant.ORDER_STATUS_FINISHED:
                        tvStatus.setText("已完成");
                        break;
                    case AppConstant.ORDER_STATUS_CLOSED:
                        tvStatus.setText("已关闭");
                        break;
                }
                break;
            case AppConstant.ORDER_TYPE_SS_PICK://自商城自提
                switch (item.getOrderStatus()) {
                    case AppConstant.ORDER_STATUS_NO_PAY:
                        tvStatus.setText("待支付");
                        tvCloseOrder.setVisibility(View.VISIBLE);
//                        tvChangePrice.setVisibility(View.VISIBLE);
                        break;
                    case AppConstant.ORDER_STATUS_NO_PICKUP:
                        tvStatus.setText("待自提");
                        break;
                    case AppConstant.ORDER_STATUS_FINISHED:
                        tvStatus.setText("已完成");
                        break;
                    case AppConstant.ORDER_STATUS_CLOSED:
                        tvStatus.setText("已关闭");
                        break;
                }
                break;
            case AppConstant.ORDER_TYPE_SS_CHECKOUT://收银台
                switch (item.getOrderStatus()) {
                    case AppConstant.ORDER_STATUS_NO_PAY:
                        tvStatus.setText("待支付");
                        tvCloseOrder.setVisibility(View.VISIBLE);
                        break;
                    case AppConstant.ORDER_STATUS_FINISHED:
                        tvStatus.setText("已完成");
                        break;
                    case AppConstant.ORDER_STATUS_CLOSED:
                        tvStatus.setText("已关闭");
                        break;
                }
                break;
        }

        tvCheckDelivery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnActionClickListener != null) {
                    mOnActionClickListener.onCheckDelivery(item);
                }
            }
        });
        tvDeliveryGoods.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnActionClickListener != null) {
                    mOnActionClickListener.onSendGoods(item);
                }
            }
        });
        tvCloseOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnActionClickListener != null) {
                    mOnActionClickListener.closeOrder(item);
                }
            }
        });
        tvChangePrice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnActionClickListener != null) {
                    mOnActionClickListener.changePrice(item);
                }
            }
        });

        mAdapterGoodsInOrderCommon.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (mOnActionClickListener != null) {
                    mOnActionClickListener.onItemClick(item);
                }
            }

        });
    }

    @Override
    public Paint dividerPaint(int position, RecyclerView parent) {
        Paint paint = new Paint();
        paint.setColor(mContext.getResources().getColor(R.color.sg_common_line_bg));
        paint.setStrokeWidth(10);
        return paint;
    }

    public interface OnActionClickListener {
        void onCheckDelivery(OrderListBean bean);

        void onSendGoods(OrderListBean bean);

        void closeOrder(OrderListBean bean);

        void onItemClick(OrderListBean bean);

        void changePrice(OrderListBean bean);
    }

    private OnActionClickListener mOnActionClickListener;

    public void setOnActionClickListener(OnActionClickListener listener) {
        mOnActionClickListener = listener;
    }
}
